package cn.xfdzx.android.apps.shop.activity;


import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.AfterSalesListRetailAdapter;
import cn.xfdzx.android.apps.shop.adapter.AfterServiceMarketGoodsAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.AfterMarketSelGoodsBean;
import cn.xfdzx.android.apps.shop.bean.AfterSalesListMarketBean;
import cn.xfdzx.android.apps.shop.bean.AfterSalesListRetailBean;
import cn.xfdzx.android.apps.shop.bean.ConfirmBeen;
import cn.xfdzx.android.apps.shop.bean.NewOrderBeen;
import cn.xfdzx.android.apps.shop.bean.OrderListBeen;
import cn.xfdzx.android.apps.shop.bean.ReturnReasonBean;
import cn.xfdzx.android.apps.shop.contract.OrderListContract;
import cn.xfdzx.android.apps.shop.presenter.OrderListPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.Utils;

/*批发申请退款商品列表选择页面*/
public class AfterServiceMaraketGoodsActivity extends BaseMvpActivity<OrderListPresenter> implements OrderListContract.View {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.recycle_goods)
    RecyclerView recycle_goods;
    @BindView(R.id.checkbox_all)
    CheckBox checkbox_all;
    @BindView(R.id.tv_sure)
    TextView tv_sure;//
    private AfterServiceMarketGoodsAdapter goodsAdapter;
    private List<AfterSalesListRetailBean.DataBean.ApplyBean> applyBeanList = new ArrayList<>();//商品列表
    private OrderListBeen.DataBean dataBean;
    private List<OrderListBeen.DataBean.CartInfoBean> cartInfo;
    private boolean isAllCheck = false;
    private List<AfterMarketSelGoodsBean> selGoodsBeans = new ArrayList<>();
    @Override
    public int getLayoutId() {
        return R.layout.activity_after_marketlist;
    }

    @Override
    public void initView() {
        mPresenter = new OrderListPresenter();
        mPresenter.attachView(this);
        dataBean = (OrderListBeen.DataBean) getIntent().getSerializableExtra("data");
        recycle_goods.setLayoutManager(new LinearLayoutManager(this));
        cartInfo = dataBean.getCartInfo();
        goodsAdapter = new AfterServiceMarketGoodsAdapter(cartInfo);
        recycle_goods.setAdapter(goodsAdapter);

        initListener();
    }

    private void initListener() {

        checkbox_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!buttonView.isPressed()) { //判断view是否是用户点击
                    return;
                }
                for (int i = 0; i < cartInfo.size(); i++) {
                    cartInfo.get(i).setAfterCheck(isChecked);
                }
                goodsAdapter.notifyDataSetChanged();
            }
        });

        goodsAdapter.setItemCheckListener(new AfterServiceMarketGoodsAdapter.OnItemOnClickListener() {
            @Override
            public void onItemCheckClick(int position, OrderListBeen.DataBean.CartInfoBean date) {
                for (int i = 0; i < cartInfo.size(); i++) {
                    if (cartInfo.get(i).isAfterCheck()){
                        isAllCheck = true;
                    }else {
                        isAllCheck = false;
                        break;
                    }
                }

                checkbox_all.setChecked(isAllCheck);
            }
        });
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {
        Log.e("异常", throwable.toString());
    }

    @Override
    public void onOrderListSuccess(OrderListBeen bean) {

    }

    @Override
    public void getRefundRreasonSuccess(ReturnReasonBean bean) {

    }

    @Override
    public void getAfterServiceRequestSuccess(ReturnReasonBean bean) {

    }

    @Override
    public void getReturnReasonSuccess(ReturnReasonBean bean) {

    }

    @Override
    public void finishRefresh() {
        //mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        //mRefreshLayout.finishLoadMore(true);//加载完成
    }

    @Override
    public void onPaySuccess(NewOrderBeen bean, int position) {

    }

    @Override
    public void onConfirmSuccess(ConfirmBeen bean, int position) {

    }

    @Override
    public void onGetAfterSalesRetailListSuccess(AfterSalesListRetailBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onGetIsExceedTimeSuccess(AfterSalesListRetailBean bean) {

    }

    @Override
    public void onGetAfterSalesMarketListSuccess(AfterSalesListMarketBean bean) {

    }


    @OnClick({R.id.iv_back,R.id.tv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;
            case R.id.tv_sure:    //确定按钮点击
                selGoodsBeans.clear();
                for (int i = 0; i < cartInfo.size(); i++) {
                    if (cartInfo.get(i).isAfterCheck()){
                        selGoodsBeans.add(new AfterMarketSelGoodsBean(
                                cartInfo.get(i).getProduct_id()+"",
                                cartInfo.get(i).getProductInfo().getStore_name(),
                                cartInfo.get(i).getProductInfo().getUnit_name(),
                                cartInfo.get(i).getProductInfo().getImage(),
                                cartInfo.get(i).getCart_num(),1,
                                Double.parseDouble(cartInfo.get(i).getDiscountPrice()) == 0?cartInfo.get(i).getMarketPrice():cartInfo.get(i).getDiscountPrice()));
                    }
                }

                if (EmptyUtil.isEmpty(selGoodsBeans)){
                    Utils.toastMessage(this,"请选择要售后的商品");
                    return;
                }

                Intent intent = new Intent(this,AfterServiceMaraketGoods_RequestActivity.class);
                intent.putExtra("seldate", (Serializable) selGoodsBeans);
                intent.putExtra("orderId", dataBean.getOrder_id());
                intent.putExtra("data", dataBean);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
